package com.wr.springbootadmin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.wr.springbootadmin.pojo.PicPojo;
import com.wr.springbootadmin.service.HikApkService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.*;

/**
 * @author 王瑞
 * @description: TODO
 * @date 2021/4/20  15:37
 */
@Service
public class HikApkServiceImpl implements HikApkService {

    @Override
    public List<PicPojo> getImgUrlByIndexCodeList(List<String> indexCodeList, String appKey, String appSecret, String host) {
        ArtemisConfig.appKey = appKey;
        ArtemisConfig.appSecret = appSecret;
        ArtemisConfig.host = host;

        final String getSecurityApi = "/artemis" +  "/api/video/v1/pictureInfos"; // 接口路径


        List<List<String>> lists = subList(indexCodeList);

        Map<String, String> path = new HashMap<String, String>(2) {{
            put("https://", getSecurityApi);
        }};

        List<PicPojo> picPojos = new ArrayList<>();
        for (List<String> list : lists) {
            Map<String, Object> parem = new HashMap<>();
            parem.put("pageNo", 1);
            parem.put("pageSize", list.size());
            parem.put("cameraIndexCodes", list);
            putTime(parem);
            String result = ArtemisHttpUtil.doPostStringArtemis(path, JSON.toJSONString(parem), null, null, "application/json");

            JSONObject jsonObject = JSON.parseObject(result);
            if (result != null) {
                String data = jsonObject.getString("data");
                String msg = jsonObject.getString("msg");
                if ("success".equals(msg)) {
                    JSONObject jsonObject1 = JSON.parseObject(data);
                    String picUrlList = jsonObject1.getString("list");
                    List<PicPojo> picPojos1 = JSON.parseArray(picUrlList, PicPojo.class);
                    picPojos.addAll(picPojos1);
                }
            }
        }
        picPojos.forEach(picPojo -> System.out.println(picPojo));

        return picPojos;

    }


    public List<List<String>> subList(List<String> list){
        int size = list.size();
        List<List<String>> li = new ArrayList<>();
        int toIndex = 1;
        int listSize = list.size();
        for (int i = 0; i < size; i += toIndex) {
            if (i + toIndex > size) {
                toIndex = listSize - i;
            }
            List<String> list1 = list.subList(i, i + toIndex);
            li.add(list1);
        }
        return li;

    }

    public void putTime(Map<String, Object> parem) {
        Calendar calendar = Calendar.getInstance();
        /* HOUR_OF_DAY 指示一天中的小时 */
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTimeStr = df.format(calendar.getTime());
        String endTimeStr = df.format(new Date());
        String startTime = getISO8601TimestampFromDateStr(startTimeStr);
        String endTime = getISO8601TimestampFromDateStr(endTimeStr);
        parem.put("startTime", startTime);
        parem.put("endTime", endTime);

    }



    public String getISO8601TimestampFromDateStr(String timestamp) {
        java.time.format.DateTimeFormatter dtf1 = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse(timestamp, dtf1);
        ZoneOffset offset = ZoneOffset.of("+08:00");
        OffsetDateTime date = OffsetDateTime.of(ldt, offset);
        java.time.format.DateTimeFormatter dtf2 = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        return date.format(dtf2);
    }
}
